package com.imay.base.util;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by jeaman on 2017/4/19.
 */

public class DateUtil {
    /** 时间日期格式化到年月日时分秒. */
    public static final String dateFormatYMDHMS = "yyyy-MM-dd HH:mm:ss";

    /** 时间日期格式化到年月日. */
    public static final String dateFormatYMD = "yyyy-MM-dd";

    /** 时间日期格式化到年月. */
    public static final String dateFormatYM = "yyyy-MM";

    /** 时间日期格式化到年月日时分. */
    public static final String dateFormatYMDHM = "yyyy-MM-dd HH:mm";

    /** 时间日期格式化到年月日时分. */
    public static final String dateFormatMDHM = "MM-dd HH:mm";

    /** 时间日期格式化到月日. */
    public static final String dateFormatMD = "MM/dd";

    /** 时间日期格式化到月日 */
    public static final String dateFormatMD2 = "MM-dd";

    /** 时分秒. */
    public static final String dateFormatHMS = "HH:mm:ss";

    /** 时分. */
    public static final String dateFormatHM = "HH:mm";

    /** 上午. */
    public static final String AM = "AM";

    /** 下午. */
    public static final String PM = "PM";

    //秒转换为毫秒
    private static long second2Millisecond(long second) {
        return second < 10000000000L ? second * 1000L : second;
    }

    //将秒转换为时分秒
    public static String getTimeBySecond(long second) {
        return String.format("%02d:%02d:%02d", second / 3600, (second / 60) % 60, second % 60);
    }

    /**
     * 描述：秒转化为String类型.
     *
     * @param second
     * @param format
     * @return String
     */
    public static String getStringBySecond(long second, String format) {
        return getStringByMillisecond(second2Millisecond(second), format);
    }

    public static String getStringByMillisecond(long millisecond, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        String strDate = null;
        try {
            strDate = simpleDateFormat.format(millisecond);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strDate;
    }
}
